<?php session_start();
include "../constants.php";
$newip = $_SERVER['REMOTE_ADDR'];
$Employee=$_SESSION['Username'];

if(!isset($Employee)||empty($Employee)||$newip!= $_SESSION['IP'])
{echo "If you are payday loan applicant, please start your ". "<a href='apply.php'>application here</a> now, thank you for your cooperation!";
exit();
}

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 

if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db(DB_NAME);


$status = $_GET['status'];
$startmonth = $_GET['startmonth'];
$endmonth = $_GET['endmonth'];
	// Escape User Input to help prevent SQL Injection
$status = mysql_real_escape_string($mstatus);
$startmonth = mysql_real_escape_string($startmonth);
$endmonth = mysql_real_escape_string($endmonth);
if($status=="3")
{

SELECT * FROM Applications WHERE NewApp='$status' ORDER BY AccountID
$query = "SELECT Date_Format(CollectDate, '%M,%Y' ) AS DATES FROM APPLICATION
		WHERE Datediff(CollectDate , STR_TO_DATE('$startmonth','%m/%d/%Y' ))>=0 AND
			Datediff(CollectDate, STR_TO_DATE('$endmonth','%m/%d/%Y' ))<=0 AND NewApp='$status'
		ORDER BY Date_Format(CollectDate, '%Y' ) DESC , Date_Format(CollectDate,'%m' ) DESC, Date_Format(CollectDate,'%d' ) DESC";

}
else if($status=="4")
{
$reports="Yearly Report";
	//build query
$query = "SELECT Date_Format(APPROVEDDATE, '%Y' ) AS DATES,COUNT(*) as TotalAPPS, 
        SUM(CASE STATUS WHEN 'DECLINED' THEN 0 WHEN 'NEW' THEN 0 ELSE 1 END) AS APPROVED_APPS, 
	SUM(CASE STATUS WHEN 'DECLINED' THEN 1 ELSE 0 END) AS DECLINED_APPS, 
        SUM(CASE STATUS WHEN 'DECLINED' THEN 0 WHEN 'NEW' THEN 0 ELSE APPROVEDLOAN END) AS APPROVED_LOANS, 
        SUM(CASE STATUS WHEN 'DECLINED' THEN 0 WHEN 'NEW' THEN 0 ELSE APPROVEDCOSTS END) AS APPROVED_CHARGES, 
        SUM(PAIDAMOUNT) AS TotalPAYMENT,
	SUM(CASE STATUS WHEN 'DECLINED' THEN 0 WHEN 'NEW' THEN 0 ELSE PAIDAMOUNT-APPROVEDLOAN END) AS REVENUES
FROM APPLICATION
WHERE Datediff(APPROVEDDATE , STR_TO_DATE('$startmonth','%m/%d/%Y' ))>=0 AND
			Datediff(APPROVEDDATE, STR_TO_DATE('$endmonth','%m/%d/%Y' ))<=0
GROUP BY Date_Format(APPROVEDDATE, '%Y' )
ORDER BY Date_Format(APPROVEDDATE, '%Y' ) DESC";

}

	
$qry_result = mysql_query($query) or die(mysql_error());

	//Build Result String
$display_string = "<table><table cellspacing='4' cellpadding='1' border='1' style='margin: 0 0 0 0;'>";
$display_string .= "<tr><th width='700' align='center'>Please select accountID</th></tr>";

// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
	$display_string .= "<tr>";
	$display_string .= "<td  width='700' align='center'>";
	$display_string .= "<select style='width: 220px;' name='accountid' onChange='autoSubmit();'>";
	$display_string .= "<option value='null'>Please select Application AccountID</option>";
	$display_string .= "<option value='1'>Please select Application AccountID</option>";
	$display_string .= "</td></tr>";
//	$display_string .= "<td  width='700' align='center'>$row[DATES]</td>";


}
//echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>